﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using LitJson;

public class LoginPanel : MonoBehaviour
{
    public InputField inputField;

    public static System.Action<string,bool> loginSuccess;

    string inputName = "";

    void Awake()
    {
        loginSuccess += OnLogin;
    }

    private void OnDestroy()
    {
        loginSuccess -= OnLogin;
    }

    public void OnClick()
    {
        string username = inputField.text;
        Color color = new Color(Random.value, Random.value, Random.value);

        //Dictionary<string, string> data = new Dictionary<string, string>();
        //data["username"] = username;
        //data["color"] = JsonUtility.ToJson(color);

        UserData data = new UserData(username,color);

        inputName = username;

        string json = JsonUtility.ToJson(data);
        NetworkManager.Instance.SendMessage(Protocol.Login_CMD, json);
    }

    void OnLogin(string json,bool islocal)
    {
        gameObject.SetActive(false);
        if (GameObject.Find("Main Camera"))
        {
            GameObject.Find("Main Camera").SetActive(false);
        }
        
    }
}
